package server;

public class Player implements Comparable<Player> {

	private String username;
	private long timeUsed = 0;
	protected ServerSocketThread socketThread;
	private int correctAnswers = 0;
	
	
	public Player(String username, ServerSocketThread socketThread){
		this.username = new String(username);
		this.socketThread = socketThread;
	}

	public String getUsername() {
		return username;
	}
	
	public void addTime(long time) {
		timeUsed += time;
	}
	
	public long getTime(){
		return timeUsed;
	}
	
	public void addCorrectAnswer(){
		correctAnswers++;
	}
	
	public int getCorrectAnswers(){
		return correctAnswers;
	}
	
	/**
	 * Resets the number of correct answers and the time used.
	 */
	public void resetAnswers(){
		correctAnswers = 0;
		timeUsed = 0;
	}
	
	public int hashCode(){
		return username.hashCode();
	}

	@Override
	public int compareTo(Player o) {
		
		if (getCorrectAnswers() < o.getCorrectAnswers()){
			return 1;
			
		} else if (getCorrectAnswers() > o.getCorrectAnswers()){
			return -1;
			
		} else { // equal number of correct answers. take a look at the time used
			if (getTime() < o.getTime())
				return 1;
			else if (getTime() > o.getTime())
				return -1;
			
		}
		/*
		 *  Both players have used the exact same amount of milliseconds to answer.
		 *  They should have a medal!
		 */
		
		return 0;
	}
}
